package com.hidtechs.popfax.xml;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLContactsHandler extends DefaultHandler{ 
	// =========================================================== 
	// Fields 
	// =========================================================== 
	private boolean apiresult = false;
	private boolean authentication = false;
	private boolean result = false;
	private boolean getaddressbook = false;
	private boolean contact = false;
	private boolean id = false;
	private boolean number = false;
	private boolean gsmnumber = false;
	private boolean name = false;
	private boolean error = false;
	
	private ParsedContactsDataSet obj;
	public ArrayList<Object> arrayOfObject;
	
	
	
	@Override 
	public void startDocument() throws SAXException { 
		arrayOfObject = new ArrayList<Object>(); 
	} 
	
	
	@Override 
	public void endDocument() throws SAXException { 
	 
	} 

	/** Gets be called on opening tags like: 
	 * <tag> 
	 * Can provide attribute(s), when xml was like: 
	 * <tag attribute="attributeValue">*/ 
	@Override 
	public void startElement(String namespaceURI, String localName, 
			String qName, Attributes atts) throws SAXException { 
		if (localName.equals("API_RESULT")) { 
	
			obj = new ParsedContactsDataSet();
			this.apiresult = true; 
		}else if (localName.equals("AUTHENTICATION")) {
	
			this.authentication = true; 
		}else if (localName.equals("RESULT") ) { 
	
			this.result = true; 
		}	else if (localName.equals("CONTACT") ) { 
	
			obj = new ParsedContactsDataSet();
			this.contact = true; 
		}	else if (localName.equals("ID") ) { 
	
	
			this.id = true; 
		}	else if (localName.equals("NUMBER") ) { 
	
			this.number = true; 
		}	else if (localName.equals("GSMNUMBER") ) { 
	
			this.gsmnumber = true; 
		}	else if (localName.equals("NAME") ) { 
	
			this.name = true; 
		}	else if (localName.equals("ERROR") ) { 
	
			this.error = true; 
		}
	}
	/** Gets be called on closing tags like: 
	 * </tag> */ 
	@Override 
	public void endElement(String namespaceURI, String localName, String qName) 
	throws SAXException { 
		if (localName.equals("API_RESULT")) { 
	
			this.apiresult = false; 
		}else if (localName.equals("AUTHENTICATION")) {
	
			this.authentication = false; 
		}else if (localName.equals("RESULT") ) { 
				this.result = false;      
		}else if (localName.equals("CONTACT") ) { 
	
			arrayOfObject.add(obj);
			this.contact = false; 
		}else if (localName.equals("ID") ) { 
			this.id = false; 
		}else if (localName.equals("NUMBER") ) { 
	
			this.number = false; 
		}else if (localName.equals("GSMNUMBER") ) { 
	
			this.gsmnumber = false; 
		}else if (localName.equals("NAME") ) { 
	
			this.name = false; 
		}else if (localName.equals("ERROR") ) { 
	
			this.error = false; 
		}
	} 

	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */ 
	@Override 
	public void characters(char ch[], int start, int length) { 
		if(this.result){ 
	
			obj.setResult(Integer.parseInt((new String(ch, start, length)))); 
		}else if (this.id){ 
	
			obj.setId(new String(ch, start, length)); 
		}else if (this.number){ 
	
			obj.setNumber(new String(ch, start, length)); 
		}else if (this.gsmnumber){ 
	
			obj.setGsmnumber(new String(ch, start, length)); 
		}else if (this.name){ 
	
			obj.setName(new String(ch, start, length)); 
		}else if (this.error){ 
	
			obj.setError(Integer.parseInt((new String(ch, start, length)))); 
		}
	} 
}
